<?php

class Jenis_pinj extends Controller {

	function Jenis_pinj()
	{
		parent::Controller();	
		$this->load->library('auth');
		$this->auth->check_user_authentification();
		$this->load->model('jenis_pinj_model');		
	}
	
	function index()
	{
		$data['title'] = "Jenis Pinjaman";
		$data['main_content'] = 'jenis_pinj/display';
		$data['jenis_pinj_data'] = $this->jenis_pinj_model->get_all_data();		
		$this->load->view('layout/template', $data);
	}

	function add()
	{
		$data['title'] = "Tambah Jenis Pinjaman";
		$data['main_content'] = 'jenis_pinj/form';
		$data['act'] = 'add';
		$data['form_act'] = 'insert';
		$data['jenis_pinj_data'] = FALSE;
		$data['new_id'] = $this->jenis_pinj_model->get_last_id();
		$this->load->view('layout/template', $data);
	}

	function view()
	{
		$id = $this->uri->segment(3);
		$data['title'] = "Lihat Jenis Pinjaman";
		$data['main_content'] = 'jenis_pinj/form';
		$data['act'] = 'view';
		$data['form_act'] = '';
		$data['jenis_pinj_data'] = $this->jenis_pinj_model->get_data_by_id($id);
		$this->load->view('layout/template', $data);
	}	

	function edit()
	{
		$id = $this->uri->segment(3);
		$data['title'] = "Edit Jenis Pinjaman";
		$data['main_content'] = 'jenis_pinj/form';
		$data['act'] = 'edit';
		$data['form_act'] = 'update/'.$id;
		$data['jenis_pinj_data'] = $this->jenis_pinj_model->get_data_by_id($id);
		$this->load->view('layout/template', $data);
	}
	
	function popup()
	{
		$data['jenis_pinj_data'] = $this->jenis_pinj_model->get_all_data();
		$this->load->view('jenis_pinj/popup', $data);
	}

	function insert()
	{
		if (!$this->_jenis_pinj_validation())
		{
			$this->session->set_userdata('ERRMSG_ARR', validation_errors());
			$this->add();
		}
		else
		{
			$this->jenis_pinj_model->fill_data();
			//Cek Akun
			if(!$this->jenis_pinj_model->check_name())
			{
				$this->session->set_userdata('ERRMSG_ARR', 'Jenis Pinjaman telah digunakan');
				$this->add();
			}
			//Insert Data
			elseif($this->jenis_pinj_model->insert_data()) 
			{
				$this->session->set_userdata('SUCCESSMSG', 'Jenis Pinjaman baru sukses ');
				redirect('jenis_pinj');
			}			
		}
	}

	function update()
	{
		$id = $this->uri->segment(3);
		if (!$this->_jenis_pinj_validation())
		{
			$this->session->set_userdata('ERRMSG_ARR', validation_errors());
			$this->edit();
		}
		else
		{
			$this->jenis_pinj_model->fill_data();
			//Cek Akun
			if(!$this->jenis_pinj_model->check_name($id))
			{
				$this->session->set_userdata('ERRMSG_ARR', 'Jenis Pinjaman telah digunakan');
				$this->edit();
			}
			//Update Data
			elseif($this->jenis_pinj_model->update_data($id))
			{
				$this->session->set_userdata('SUCCESSMSG', 'Update jenis pinjaman sukses ');
				redirect('jenis_pinj');
			}
		}
	}

	function delete()
	{	
		$id = $this->input->post('id');
		$jenis_pinj_data = $this->jenis_pinj_model->get_data_by_id($id);		
		
			if($this->jenis_pinj_model->delete_data($id))
			{
				$msg = 'S#Jenis_pinj '.$jenis_pinj_data['id_jenis_pinj'].' telah dihapus.';
			}
			else
			{
				$msg .= 'E#Terjadi kesalahan dalam menghapus data jenis pinjanan '.$jenis_pinj_data['id_jenis_pinj'].'. Harap coba lagi.';
			}
			echo $msg;
	}

	function _jenis_pinj_validation()
	{	
		$this->form_validation->set_rules('id_jenis_pinj', 'Jenis Pinjaman', 'trim|required');		
			
		return $this->form_validation->run();
	}

}
/* End of file jenis_pinj.php */
/* Location: ./application/controllers/jenis_pinj.php */
